package com.CNMedical.web.controller.system.applet;

import com.CNMedical.common.annotation.Anonymous;
import com.CNMedical.common.core.controller.BaseController;
import com.CNMedical.common.core.domain.R;
import com.CNMedical.common.utils.StringUtils;
import com.CNMedical.system.domain.entity.AppletUser;
import com.CNMedical.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户Controller
 *
 * @author tang
 * @date 2023-03-29
 */
@Api(tags = "用户接口")
@Anonymous
@RestController
@RequestMapping("/applet/userControl")
public class UserController extends BaseController {

    @Resource
    private IAppletUserService appletUserService;


    /**
     * 修改个人信息
     */
    @ApiOperation("修改个人信息")
    @PostMapping("/editAppletUserDetail")
    public R editUserDetail(@Validated @RequestBody AppletUser user)
    {
        if (appletUserService.checkUserNameUnique(user))
        {
            //return R.fail("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && appletUserService.checkPhoneUnique(user))
        {
            return R.fail("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && appletUserService.checkEmailUnique(user))
        {
            return R.fail("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return R.ok(appletUserService.editAppletUser(user));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @ApiOperation("根据用户编号获取详细信息")
    @GetMapping(value = { "/getAppletUserDetail/{userId}" })
    public R getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AppletUser appletUser = appletUserService.selectAppletUserById(userId);
        return R.ok(appletUser);
    }

}
